package licode.unisop.single.user.application.service;

import cn.hutool.core.bean.BeanUtil;
import licode.unisop.realm.client.api.SiRealmProviderQueryService;
import licode.unisop.realm.client.dto.SiRealmProviderDTO;
import licode.unisop.single.user.application.entity.ProviderEntity;
import licode.unisop.single.user.application.gateway.ProviderDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 领域查询接口实现
 *
 * @author licode
 */
@Component
public class SiRealmProviderQueryServiceDefault implements SiRealmProviderQueryService {
    @Resource
    private ProviderDao providerDao;

    @Override
    public SiRealmProviderDTO getRealmProviderById(String id) {
        ProviderEntity entity = providerDao.getWithId(id);
        return entity == null ? null : BeanUtil.copyProperties(entity, SiRealmProviderDTO.class);
    }

    @Override
    public List<SiRealmProviderDTO> getRealmProviderList() {
        List<SiRealmProviderDTO> realms = null;
        List<ProviderEntity> providerList = providerDao.getList();
        if (null != providerList && !providerList.isEmpty()) {
            realms = new ArrayList<>();
            for (ProviderEntity item: providerList) {
                realms.add(BeanUtil.copyProperties(item, SiRealmProviderDTO.class));
            }
        }
        return realms;
    }
}
